package ciyuan.Block.Machine.Simple.ToolMakingTable;

import ciyuan.Block.Machine.Simple.SimpleMachineRegister;
import ciyuan.Block.TypeInterface.MachineTileEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 150149
 */
public class ToolMakingTableTileEntity extends MachineTileEntity {

    private static final ITextComponent TITLE = new TranslationTextComponent("block.ciyuanrpg.tool_making_table");

    public ToolMakingTableTileEntity() {
        super(SimpleMachineRegister.TOOL_MAKING_TABLE_ENTITY.get(), 9, TITLE, 800);
    }

    public static final Map<Item, List<ItemStack>> craftMap = new HashMap<>();

    static {
        craftMap.put(Items.IRON_AXE, Arrays.asList(new ItemStack(Items.IRON_INGOT, 3), new ItemStack(Items.STICK, 2)));
        craftMap.put(Items.IRON_PICKAXE, Arrays.asList(new ItemStack(Items.STICK, 2), new ItemStack(Items.IRON_INGOT, 3)));
        craftMap.put(Items.IRON_HOE, Arrays.asList(new ItemStack(Items.STICK, 2), new ItemStack(Items.IRON_INGOT, 2)));
        craftMap.put(Items.IRON_SHOVEL, Arrays.asList(new ItemStack(Items.STICK, 2), new ItemStack(Items.IRON_INGOT, 1)));
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new ToolMakingTableContainer(SimpleMachineRegister.TOOL_MAKING_TABLE_CONTAINER.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void tick() {
        if (!this.hasLevel() || this.level.isClientSide) {
            return;
        }
        List<Item> outputs = this.getOutput();
        if (outputs.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (this.getProgress() >= this.getConsumeTime()) {
            ItemStack itemStack = new ItemStack(outputs.get(0));
            this.inventory.clearContent();
            this.inventory.addItem(itemStack);
            this.resetProgress();
        } else {
            this.addProgress();
        }
    }

    private List<Item> getOutput() {
        return craftMap.entrySet().stream()
                .parallel()
                .filter(entry -> {
                    for (int i = 0; i < entry.getValue().size(); i++) {
                        if (!ItemStack.matches(entry.getValue().get(i), this.inventory.getItem(i))) {
                            return false;
                        }
                    }
                    return true;
                })
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }

}
